package com.hngy.xpq.android_notebook.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hngy.xpq.android_notebook.bean.Notepbook;
import com.hngy.xpq.android_notebook.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class SQLiteHelper extends SQLiteOpenHelper {

    private SQLiteDatabase sqLiteDatabase;

    public SQLiteHelper(Context context){
        super(context, DBUtil.DATABASE_NAME, null, DBUtil.DATABASE_VERION);
        sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+ DBUtil.DATABASE_TABLE+"("+ DBUtil.NOTEPAD_ID+
                " integer primary key autoincrement,"+ DBUtil.NOTEPAD_TEXT +
                " text," + DBUtil.NOTEPAD_TIME+ " text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    //添加记事本
    public boolean insertData(String userContent,String userTime){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DBUtil.NOTEPAD_TEXT,userContent);
        contentValues.put(DBUtil.NOTEPAD_TIME,userTime);
        return
                sqLiteDatabase.insert(DBUtil.DATABASE_TABLE,null,contentValues)>0;
    }

    //删除记事本
    public boolean deleteData(String id){
        String sql= DBUtil.NOTEPAD_ID+"=?";
        String[] contentValuesArray=new String[]{String.valueOf(id)};
        return
                sqLiteDatabase.delete(DBUtil.DATABASE_TABLE,sql,contentValuesArray)>0;
    }

    //修改记事本
    public boolean updateData(String id,String content,String userYear){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DBUtil.NOTEPAD_TEXT,content);
        contentValues.put(DBUtil.NOTEPAD_TIME,userYear);
        String sql= DBUtil.NOTEPAD_ID+"=?";
        String[] strings=new String[]{id};
        return
                sqLiteDatabase.update(DBUtil.DATABASE_TABLE,contentValues,sql,strings)>0;
    }

    //页面显示查询
    public List<Notepbook> query(){
        List<Notepbook> list=new ArrayList<Notepbook>();
        Cursor cursor=sqLiteDatabase.query(DBUtil.DATABASE_TABLE,null,null,null,
                null,null, DBUtil.NOTEPAD_ID+" desc");
        if (cursor!=null){
            while (cursor.moveToNext()){
                Notepbook noteInfo=new Notepbook();
                String id = String.valueOf(cursor.getInt
                        (cursor.getColumnIndex(DBUtil.NOTEPAD_ID)));
                String content = cursor.getString(cursor.getColumnIndex
                        (DBUtil.NOTEPAD_TEXT));
                String time = cursor.getString(cursor.getColumnIndex
                        (DBUtil.NOTEPAD_TIME));
                noteInfo.setId(id);
                noteInfo.setNotepadContent(content);
                noteInfo.setNotepadTime(time);
                list.add(noteInfo);
            }
            cursor.close();
        }
        return list;
    }

    //根据文本内容查找记事本
    public List<Notepbook> query2(String text){
        List<Notepbook> list=new ArrayList<Notepbook>();
        Cursor cursor=sqLiteDatabase.rawQuery("select * from "+ DBUtil.DATABASE_TABLE + " where " + DBUtil.NOTEPAD_TEXT + " like ?" , new String[]{"%" + text +"%"} , null );
        if (cursor!=null){
            while (cursor.moveToNext()){
                Notepbook noteInfo=new Notepbook();
                String id = String.valueOf(cursor.getInt
                        (cursor.getColumnIndex(DBUtil.NOTEPAD_ID)));
                String content = cursor.getString(cursor.getColumnIndex
                        (DBUtil.NOTEPAD_TEXT));
                String time = cursor.getString(cursor.getColumnIndex
                        (DBUtil.NOTEPAD_TIME));
                noteInfo.setId(id);
                noteInfo.setNotepadContent(content);
                noteInfo.setNotepadTime(time);
                list.add(noteInfo);
            }
            cursor.close();
        }
        return list;
    }
}
